package com.keta.foundation.tag.dwz;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.keta.foundation.entity.Module;

public class MenuTag extends SimpleTagSupport {

	private List<Module> menuData;
	private String urlPrefix;

	@Override
	public void doTag() throws JspException, IOException {

		getJspContext().getOut().write(renderMenu());

		super.doTag();
	}

	private String renderMenu() {

		StringBuilder buffer = new StringBuilder();
		buffer.append("<div class=\"toggleCollapse\"><h2>主菜单</h2><div>收缩</div></div>");
		buffer.append("<div class=\"accordion\" fillSpace=\"sidebar\">");
		for (Module menu : menuData) {
			buffer.append("<div class=\"accordionHeader\">");
			buffer.append("<h2><span>Folder</span>");
			buffer.append(menu.getName());
			buffer.append("</h2>");

			buffer.append("</div>");
			buffer.append("<div class=\"accordionContent\">");
			buffer.append("<ul class=\"tree treeFolder\">");

			for (Module childMenu : menu.getChildren()) {

				buffer.append(rendLiTag(childMenu));

			}
			buffer.append("</ul>");
			buffer.append("</div>");
		}
		buffer.append("</div>");

		return buffer.toString();
	}

	private String rendLiTag(Module childMenu) {
		StringBuilder buffer = new StringBuilder();

		if (childMenu.getChildren().isEmpty()) {
			buffer.append("<li>");
			buffer.append(link(childMenu));
			buffer.append("</li>");
			return buffer.toString();
		} else {
			buffer.append("<li>");
			buffer.append(link(childMenu));
			buffer.append("<ul>");

			for (Module menu : childMenu.getChildren()) {
				buffer.append(rendLiTag(menu));
			}
			buffer.append("</ul>");
			buffer.append("</li>");
		}

		return buffer.toString();
	}

	private String link(Module module) {

		StringBuilder buffer = new StringBuilder();

		// 打开外部链接
		if (module.getUrl().startsWith("http:") || module.getUrl().startsWith("https:")) {
			buffer.append("<a href=\"" + module.getUrl() + "\" target=\"navTab\" external=\"true\" rel=\""
					+ module.getSn() + "_" + module.getId() + "\">" + module.getName() + "</a>\n");
		} else if (module.getUrl().startsWith("/") || module.getUrl().startsWith("\\")) {
			// 打开内部链接
			buffer.append("<a href=\"" + urlPrefix + module.getUrl() + "\" target=\"navTab\"  rel=\"" + module.getSn()
					+ "_" + module.getId() + "\">" + module.getName() + "</a>\n");
		} else if (module.getUrl().endsWith("#")) {
			// 当前链接
			buffer.append("<a href=\"" + module.getUrl() + "\" target=\"navTab\"  rel=\"" + module.getSn() + "_"
					+ module.getId() + "\">" + module.getName() + "</a>\n");
		} else {
			// 打开内部链接，自动补齐'/'
			buffer.append("<a href=\"" + urlPrefix + "/" + module.getUrl() + "\" target=\"navTab\"  rel=\""
					+ module.getSn() + "_" + module.getId() + "\">" + module.getName() + "</a>\n");
		}

		return buffer.toString();
	}

	public List<Module> getMenuData() {
		return menuData;
	}

	public void setMenuData(List<Module> menuData) {
		this.menuData = menuData;
	}

	public String getUrlPrefix() {
		return urlPrefix;
	}

	public void setUrlPrefix(String urlPrefix) {
		this.urlPrefix = urlPrefix;
	}
}
